﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NutriFast
{
    public partial class DietaSelReceta: Form
    {

        public static int idDiet, nMeals, progreso;
        public float Kcal, porcentaje, y;
        public int x;
        int currentpacient;
        public DietaSelReceta(int c,int idD, int nM)
        {
            this.currentpacient = c;
            nMeals = nM;
            idDiet = idD;
            progreso = 0;
            Kcal = dbPeri.Kcal(idDiet);
            porcentaje = dbPeri.porcentage(1, idDiet);
            InitializeComponent();
            for (int i = 0; i < nMeals; i++)
                combo.Items.Add("Comida "+(i+1));

            progressBar1.Maximum = (int)(Kcal * (porcentaje/100));
            label13.Text = "" + progressBar1.Maximum;
            progressBar2.Maximum = (int)(Kcal *( porcentaje/100));
            combo.SelectedIndex = 0;
            llenarcombobox();
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: esta línea de código carga datos en la tabla 'dsPeriquinAllFood.Food' Puede moverla o quitarla según sea necesario.
            this.foodTableAdapter.Fill(this.dsPeriquinAllFood.Food);

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            try
            {
                this.spPeriquinSearchFoodKcalUnitsTableAdapter1.Fill(this.dsPeriquinSearchFoodKcalUnits.spPeriquinSearchFoodKcalUnits, textBox1.Text);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

        }

        private void button9_Click(object sender, EventArgs e)
        {
            int idMeal =(combo.SelectedIndex + 1);
            

                if (grid2.Rows.Count >= 1)
                {
                    for (int i = 0; i < grid2.Rows.Count; i++)
                    {
                        try
                        {
                            if (dbPeri.AddRecetaMeal(int.Parse("" + grid2.Rows[i].Cells[0].Value), idMeal, idDiet))
                                button2.Enabled = true;

                        }
                        catch (System.Exception ex)
                        {
                            System.Windows.Forms.MessageBox.Show(ex.Message);
                        }
                    }//fin del for
                    
                    for (int i = 0; i < grid2.Rows.Count; i++)
                    {
                        grid2.Rows.RemoveAt(i);

                    }

                }//fin del if

                if (grid6.Rows.Count >= 1)
                {
                    for (int i = 0; i < grid6.Rows.Count; i++)
                    {
                        try
                        {
                            if (dbPeri.AddMealFood(int.Parse("" + grid6.Rows[i].Cells[0].Value), idMeal, idDiet))
                                button2.Enabled = true;

                        }
                        catch (System.Exception ex)
                        {
                            System.Windows.Forms.MessageBox.Show(ex.Message);
                        }
                    }//fin del for

                    for (int i = 0; i < grid6.Rows.Count; i++)
                    {
                        grid6.Rows.RemoveAt(i);

                    }

                }//fin del if
              

        }
        
        private void button3_Click(object sender, EventArgs e)
        {
            if (grid1.Rows.Count >= 1)
            {

                
                int n = grid6.Rows.Add();
                int index = grid1.CurrentRow.Index;
                grid6.Rows[n].Cells[0].Value = grid1.Rows[index].Cells[0].Value.ToString();
                grid6.Rows[n].Cells[1].Value = grid1.Rows[index].Cells[1].Value.ToString();
                grid6.Rows[n].Cells[2].Value = grid1.Rows[index].Cells[2].Value.ToString();
                grid6.Rows[n].Cells[3].Value = grid1.Rows[index].Cells[3].Value.ToString();
                grid6.Rows[n].Cells[4].Value = grid1.Rows[index].Cells[4].Value.ToString();
                progreso = progreso +(int) ((double)grid1.Rows[index].Cells[4].Value);
                


                grid1.Rows.RemoveAt(grid1.CurrentRow.Index);

                if (progreso < progressBar1.Maximum)
                {
                    progressBar1.Value = (int)progreso;
                    progressBar2.Value = 0;
                    label13.BackColor = System.Drawing.SystemColors.Control;
                    label14.Text = "" + 0;
                }
                else
                {
                    label14.Text = "" + (progreso - progressBar1.Maximum);
                    label13.Text =""+ progressBar1.Maximum;
                    label13.BackColor = Color.Red;
                    progressBar1.Value = progressBar1.Maximum;
                    if (progreso < (progressBar2.Maximum + progressBar1.Maximum))
                        progressBar2.Value = (int)progreso - progressBar1.Maximum;
                    else
                        progressBar2.Value = progressBar2.Maximum;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (grid6.Rows.Count !=0)
            {
                int index = grid6.CurrentRow.Index;
                int menos =int.Parse(grid6.Rows[index].Cells[4].Value.ToString());
                progreso = progreso- menos;
                if (progreso > progressBar1.Maximum)
                {
                    label13.Text = "" + progressBar1.Maximum;
                    label13.BackColor = Color.Red;
                    label14.Text = "" + (progreso - progressBar1.Maximum);
                    progressBar1.Value = progressBar1.Maximum;
                    if ((progreso - progressBar1.Maximum) < progressBar2.Maximum)
                        progressBar2.Value = progreso - progressBar1.Maximum;
                    else
                        progressBar2.Value = progressBar2.Maximum;
                }
                else
                {
                    progressBar1.Value = progreso;
                    progressBar2.Value = 0;
                    label13.BackColor = System.Drawing.SystemColors.Control;
                    label13.Text = "" + progreso;
                    label14.Text = "" + 0;
                }
                    grid6.Rows.RemoveAt(index);
                
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (grid2.Rows.Count > 0)
            {
                grid2.Rows.RemoveAt(grid2.CurrentRow.Index);
            }
            //else
           // {
             //   dataGridView1.Rows.Clear();
            //}
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (grid2.Rows.Count > 0)
            {
                grid2.Rows.RemoveAt(grid2.CurrentRow.Index);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (grid6.Rows.Count != 0)
            {
                int index = grid6.CurrentRow.Index;
                int menos = int.Parse(grid6.Rows[index].Cells[4].Value.ToString());
                progreso = progreso - menos;
                if (progreso > progressBar1.Maximum)
                {
                    label13.Text = "" + progressBar1.Maximum;
                    label13.BackColor = Color.Red;
                    label14.Text = "" + (progreso - progressBar1.Maximum);
                    progressBar1.Value = progressBar1.Maximum;
                    if ((progreso - progressBar1.Maximum) < progressBar2.Maximum)
                        progressBar2.Value = progreso - progressBar1.Maximum;
                    else
                        progressBar2.Value = progressBar2.Maximum;
                }
                else
                {
                    progressBar1.Value = progreso;
                    progressBar2.Value = 0;
                    label13.BackColor = System.Drawing.SystemColors.Control;
                    label13.Text = "" + progreso;
                    label14.Text = "" + 0;
                }
                grid6.Rows.RemoveAt(index);

            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            DietaResumen dre = new DietaResumen();
            dre.MdiParent = this.MdiParent;
            this.Hide();
            dre.Show();
        }

        private void combo_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = combo.SelectedIndex+1;
            porcentaje = dbPeri.porcentage(i, idDiet);
            progressBar1.Maximum = (int)(Kcal * (porcentaje/100));
            progressBar2.Maximum = (int)(Kcal * (porcentaje/100))/2;
            //grid1.Rows.Clear();
            grid2.Rows.Clear();
            //grid3.Rows.Clear();
            //grid4.Rows.Clear();
           // dataGridView1.Rows.Clear();
            //grid5.Rows.Clear();
            grid6.Rows.Clear();
            comboBox1.Items.Clear();
            progressBar1.Value = 0;
            progressBar2.Value = 0;
            llenarcombobox();
            //comboBox1.SelectedIndex = 0;
            textBox1.Clear();
            textBox2.Clear();
            textBox3.Clear();

        }

        public void llenarcombobox()
        {
            int i = combo.SelectedIndex+1;
           
            Meal m= new Meal(i,idDiet);
            dbPeri.GetUnitsMeal(m);
            comboBox1.Items.Clear();
            if (m.Verduras > 0)
                comboBox1.Items.Add("Verduras");
            if (m.Frutas > 0)
                comboBox1.Items.Add("Frutas");
            if (m.Csg > 0)
                comboBox1.Items.Add("Cereales sin grasa");
            if (m.Ccg > 0)
                comboBox1.Items.Add("Cereales con grasa");
            if (m.Leg > 0)
                comboBox1.Items.Add("Leguminosas");
            if (m.Caa > 0)
                comboBox1.Items.Add("Alimentos de Origen animal, muy bajo aporte de grasas");
            if (m.Cab > 0)
                comboBox1.Items.Add("Alimentos de Origen animal, bajo aporte de grasas");
            if (m.Cac > 0)
                comboBox1.Items.Add("Alimentos de Origen animal, moderado aporte de grasas");
            if (m.Cad > 0)
                comboBox1.Items.Add("Alimentos de Origen animal, alto aporte de grasas");
            if (m.Lea > 0)
                comboBox1.Items.Add("Leche descremada");
            if (m.Leb > 0)
                comboBox1.Items.Add("Leche semidescremada");
            if (m.Lec > 0)
                comboBox1.Items.Add("Leche entera");
            if (m.Leca > 0)
                comboBox1.Items.Add("Leche con azúcar");
            if (m.Grsp > 0)
                comboBox1.Items.Add("Aceites y grasas");
            if (m.Grcp > 0)
                comboBox1.Items.Add("Aceites y grasas con proteína");
            if (m.Azsgr > 0)
                comboBox1.Items.Add("Azúcares sin grasa");
            if (m.Azcgr > 0)
                comboBox1.Items.Add("Azúcares con grasa");
            if (m.Albde > 0)
                comboBox1.Items.Add("Alimentos libres de energía");
            if (m.Bebalc > 0)
                comboBox1.Items.Add("Bebidas Alcohólicas");
        }
        private void textBox2_TextChanged(object sender, EventArgs e)
        {

            try
            {
                this.spPeriquinBuscarRecetaTableAdapter.Fill(this.dsPeriquinBuscarReceta.spPeriquinBuscarReceta, textBox2.Text);
                
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        private void agregar2_Click(object sender, EventArgs e)
        {
            if (grid3.Rows.Count >= 1)
            {


                int n = grid2.Rows.Add();
                int index = grid3.CurrentRow.Index;
                grid2.Rows[n].Cells[0].Value = grid3.Rows[index].Cells[0].Value.ToString();
                grid2.Rows[n].Cells[1].Value = grid3.Rows[index].Cells[1].Value.ToString();
                //progreso = progreso + (int)((double)grid3.Rows[index].Cells[4].Value);



                grid3.Rows.RemoveAt(grid3.CurrentRow.Index);

              /**  if (progreso < progressBar1.Maximum)
                {
                    progressBar1.Value = (int)progreso;
                    progressBar2.Value = 0;
                    label13.BackColor = System.Drawing.SystemColors.Control;
                    label14.Text = "" + 0;
                }
                else
                {
                    label14.Text = "" + (progreso - progressBar1.Maximum);
                    label13.Text = "" + progressBar1.Maximum;
                    label13.BackColor = Color.Red;
                    progressBar1.Value = progressBar1.Maximum;
                    if (progreso < (progressBar2.Maximum + progressBar1.Maximum))
                        progressBar2.Value = (int)progreso - progressBar1.Maximum;
                    else
                        progressBar2.Value = progressBar2.Maximum;
                }**/
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            idgroup();
            try
            {
               
                this.spPeriquinFoodGroupsTableAdapter1.Fill(this.dsPeriquinFoodGroups.spPeriquinFoodGroups, x, textBox4.Text);
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            idgroup();
            try
            {
                this.spPeriquinFoodGroupsTableAdapter1.Fill(this.dsPeriquinFoodGroups.spPeriquinFoodGroups, x, textBox4.Text);
                
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        public void idgroup()
        {
            int i = combo.SelectedIndex + 1;

            Meal m = new Meal(i, idDiet);
            dbPeri.GetUnitsMeal(m);
            if (comboBox1.SelectedItem.ToString() == "Verduras")
            {
                x = 1;
                y = m.Verduras;
            }
            else if (comboBox1.SelectedItem.ToString() == "Frutas")
            {
                x = 2;
                y = m.Frutas;
            }
            else if (comboBox1.SelectedItem.ToString() == "Cereales sin grasa")
            {
                x = 3;
                y = m.Csg;
            }
            else if (comboBox1.SelectedItem.ToString() == "Cereales con grasa")
            {
                x = 4;
                y = m.Ccg;
            }
            else if (comboBox1.SelectedItem.ToString() == "Leguminosas")
            {
                x = 5;
                y = m.Leg;
            }
            else if (comboBox1.SelectedItem.ToString() == "Alimentos de Origen animal, muy bajo aporte de grasas")
            {
                x = 6;
                y = m.Caa;
            }
            else if (comboBox1.SelectedItem.ToString() == "Alimentos de Origen animal, bajo aporte de grasas")
            {
                x = 7;
                y = m.Cab;
            }
            else if (comboBox1.SelectedItem.ToString() == "Alimentos de Origen animal, moderado aporte de grasas")
            {
                x = 8;
                y = m.Cac;
            }
            else if (comboBox1.SelectedItem.ToString() == "Alimentos de Origen animal, alto aporte de grasas")
            {
                x = 9;
                y = m.Cad;
            }
            else if (comboBox1.SelectedItem.ToString() == "Leche descremada")
            {
                x = 10;
                y = m.Lea;
            }
            else if (comboBox1.SelectedItem.ToString() == "Leche semidescremada")
            {
                x = 11;
                y = m.Leb;
            }
            else if (comboBox1.SelectedItem.ToString() == "Leche entera")
            {
                x = 12;
                y = m.Lec;
            }
            else if (comboBox1.SelectedItem.ToString() == "Leche con azúcar")
            {
                x = 13;
                y = m.Leca;
            }
            else if (comboBox1.SelectedItem.ToString() == "Aceites y grasas")
            {
                x = 14;
                y = m.Grsp;
            }
            else if (comboBox1.SelectedItem.ToString() == "Aceites y grasas con proteína")
            {
                x = 15;
                y = m.Grcp;
            }
            else if (comboBox1.SelectedItem.ToString() == "Azúcares sin grasa")
            {
                x = 16;
                y = m.Azsgr;
            }
            else if (comboBox1.SelectedItem.ToString() == "Azúcares con grasa")
            {
                x = 17;
                y = m.Azcgr;
            }
            else if (comboBox1.SelectedItem.ToString() == "Alimentos libres de energía")
            {
                x = 18;
                y = m.Albde;
            }
            else if (comboBox1.SelectedItem.ToString() == "Bebidas Alcohólicas")
            {
                x = 19;
                y = m.Bebalc;
            }


        }

        private void agregar4_Click(object sender, EventArgs e)
        {
            if (grid5.Rows.Count >= 1)
            {
                 

                int n = grid6.Rows.Add();
                int index = grid5.CurrentRow.Index;
                grid6.Rows[n].Cells[0].Value = grid5.Rows[index].Cells[0].Value.ToString();
                grid6.Rows[n].Cells[1].Value = grid5.Rows[index].Cells[1].Value.ToString();
                grid6.Rows[n].Cells[2].Value =(y* float.Parse(grid5.Rows[index].Cells[2].Value.ToString())).ToString();
                grid6.Rows[n].Cells[3].Value = grid5.Rows[index].Cells[3].Value.ToString();
                grid6.Rows[n].Cells[4].Value = (y * float.Parse(grid5.Rows[index].Cells[4].Value.ToString())).ToString();
                progreso = progreso + (int)((double)grid5.Rows[index].Cells[4].Value);



                grid5.Rows.RemoveAt(grid5.CurrentRow.Index);

                if (progreso < progressBar1.Maximum)
                {
                    progressBar1.Value = (int)progreso;
                    progressBar2.Value = 0;
                    label13.BackColor = System.Drawing.SystemColors.Control;
                    label14.Text = "" + 0;
                }
                else
                {
                    label14.Text = "" + (progreso - progressBar1.Maximum);
                    label13.Text = "" + progressBar1.Maximum;
                    label13.BackColor = Color.Red;
                    progressBar1.Value = progressBar1.Maximum;
                    if (progreso < (progressBar2.Maximum + progressBar1.Maximum))
                        progressBar2.Value = (int)progreso - progressBar1.Maximum;
                    else
                        progressBar2.Value = progressBar2.Maximum;
                }
            }
        }


        private void grid2_SelectionChanged(object sender, EventArgs e)
        {
                 try
                {

                    this.spPeriquinRecetaIngredientesTableAdapter.Fill(this.dsPeriquinRecetaIngredientes.spPeriquinRecetaIngredientes, int.Parse(grid2.Rows[grid2.CurrentRow.Index].Cells[0].Value.ToString()));


    
                }
                catch (System.Exception ex)
                {
                }
        
        }

        private void grid2_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            try
            {

                this.spPeriquinRecetaIngredientesTableAdapter.Fill(this.dsPeriquinRecetaIngredientes.spPeriquinRecetaIngredientes, int.Parse(grid2.Rows[grid2.CurrentRow.Index].Cells[0].Value.ToString()));
                dsPeriquinRecetaIngredientes.Clear();


            }
            catch (System.Exception ex)
            {
            }
        }


        

              
           
    }
}
